package com.rmb.dataanalysis.controller;

import com.rmb.annotation.SystemLog;
import com.rmb.common.exception.ExceptionCast;
import com.rmb.common.model.response.*;
import com.rmb.common.model.state.TaskStatus;
import com.rmb.dataanalysis.service.DataAnalysisService;
import com.rmb.entity.ResponeRest;
import com.rmb.enums.LogType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLClientInfoException;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author ZhaoMuYang
 * @Date 2021/1/12 14:51
 */
@RestController
@RequestMapping("/rmb/analysis")
public class DataAnalysisController {
    @Resource
    private DataAnalysisService dataAnalysisService;

    /**
     * 获取所有数据源
     * @return 数据源集合
     */
    @GetMapping("/getAllDataSource")
    @SystemLog(description = "获取所有数据源",type = LogType.SELETE)
    public QueryResponseResult findAllDataSource(){

        QueryResult<Map<String, Object>> mapQueryResult = new QueryResult<>(dataAnalysisService.findAllDataSource(), dataAnalysisService.findAllDataSource().size());

        if(dataAnalysisService.findAllDataSource().size() == TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()){
            return new QueryResponseResult<>(CommonCode.FAIL,mapQueryResult);
        }

        return new QueryResponseResult<>(CommonCode.SUCCESS,mapQueryResult);
    }

    /**
     * 指定数据源下数据库
     * @param dataSourceId 数据源ID
     * @return 数据库
     */
    @GetMapping("/getAllDataBase/{dataSourceId}")
    @SystemLog(description = "指定数据源下数据库",type = LogType.SELETE)
    public ResponeRest findAllDataBase(@PathVariable("dataSourceId")Long dataSourceId){

        if(dataAnalysisService.findAllDataBase(dataSourceId).size()==TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()){
            return new ResponeRest(false,dataAnalysisService.findAllDataBase(dataSourceId));
        }
        return new ResponeRest(true,dataAnalysisService.findAllDataBase(dataSourceId));
    }

    /**
     * 指定数据库下所有表
     * @param dataCourseId 数据源ID
     * @param dataBaseName 数据库名称
     * @return 表集合
     */
    @GetMapping("/getTablesByDataSource")
    @SystemLog(description = "指定数据库下所有表",type = LogType.SELETE)
    public ResponeRest findTablesByDataSource(@RequestParam("dataSourseId")Long dataCourseId,
                                              @RequestParam("dataBaseName")String dataBaseName){
       if(dataAnalysisService.findTablesByDataSource(dataCourseId,dataBaseName).size()==TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()){
           return new ResponeRest(false,dataAnalysisService.findTablesByDataSource(dataCourseId,dataBaseName));
       }
        return new ResponeRest(true,dataAnalysisService.findTablesByDataSource(dataCourseId,dataBaseName));
    }
    /**
     * 执行sql查询
     * @param dataSourceId 数据源ID
     * @param sql sql语句
     * @return 查询结果
     */
    @GetMapping("/execute")
    @SystemLog(description = "执行sql查询",type = LogType.SELETE)
    public QueryResponseResult<List> executeSqlQuery(
            @RequestParam("dataSourceId") Long dataSourceId,
            @RequestParam("dataBaseName") String dataBaseName,
            @RequestParam("tableName") String tableName,
            @RequestParam("sql") String sql) throws SQLClientInfoException {

        List<Map<String, Object>> executeSqlQuery = dataAnalysisService.executeSqlQuery(dataSourceId, dataBaseName, tableName, sql);

        QueryResult<Map<String, Object>> mapQueryResult = new QueryResult<>(executeSqlQuery, executeSqlQuery.size());

        if(executeSqlQuery.size() == TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()){
            return new QueryResponseResult<>(CommonCode.SQL_WRONG_FORMAT,mapQueryResult);
        }

        return new QueryResponseResult<>(CommonCode.SUCCESS,mapQueryResult);
    }

    /**
     * BI分析记录
     * @param executeSql 执行语句
     * @param executeResult 执行结果
     * @param dataSourceId 数据源ID
     * @param dataBaseName 数据库名称
     * @param tableName 表名称
     */
    @GetMapping("/addAnalysis")
    @SystemLog(description = "BI分析记录",type = LogType.SELETE)
    void addAnalysis(@RequestParam("executeSql")String executeSql,
                     @RequestParam("executeResult")String executeResult,
                     @RequestParam("dataDataSourceId")Long dataSourceId,
                     @RequestParam("dataBaseName")String dataBaseName,
                     @RequestParam("tableName")String tableName){
        if(dataAnalysisService.addAnalysis(executeSql,executeResult,dataSourceId,dataBaseName,tableName)<TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()){
            ExceptionCast.cast(CommonCode.FAIL);
        }
    }

    @GetMapping("/getExcel")
    @SystemLog(description = "读取Excel表中的数据",type = LogType.SELETE)
    public ResponseResult getAnalysisExcel( @RequestParam("dataDataSourceId")Long dataSourceId,
                                            @RequestParam("dataBaseName")String dataBaseName,
                                            @RequestParam("tableName")String tableName){
        if(dataAnalysisService.getDataExcel(dataSourceId, dataBaseName, tableName)==null){
            return new ResponseResult(CommonCode.FAIL);
        }
        return new ResponseResult(CommonCode.SUCCESS,dataAnalysisService.getDataExcel(dataSourceId, dataBaseName, tableName));
    }
}
